'Import minib3d
Import minib3d.app
Import cal3d
Import cal3d.sknxmlloader
Import cal3d.calb3d
Import mojo
#TEXT_FILES="*.txt|*.xml|*.xrf|*.xaf|*.xmf|*.xaf|*.xsf"

Function Main()
	New Game
End



Class Game Extends MiniB3DApp

	Field cam:TCamera
	Field light:TLight	
	Field init_gl:Bool = False
	Field a:Float=0, dir:Int=0, oldTouchX:Int, oldTouchY:Int, touchBegin:Int, lr#, ud#
	Field txt:TText
	Field dt:DeltaTimer
	Field hndlr:CalLoaderHandler
	Field CB3D:Cal3dB3d
	Field Cimg:CalImage
	Field TestTexture:TTexture
	Field mesh:TMesh
	
	Method OnCreate()
	


		SetRender()	
		SetUpdateRate 30
		PreLoad("mojo_font.png")
		hndlr = New CalLoaderHandler()
		hndlr.LoadFile("http://vigilsoft.net/democal3d/test.xsf",0,New SKNCalSkeletonLoader())	
		hndlr.LoadFile("http://vigilsoft.net/democal3d/testpp.xmf",0,New SKNCalMeshLoader())		
		hndlr.LoadFile("http://vigilsoft.net/democal3d/run.xaf",0,New SKNCalAnimationLoader())		
		hndlr.LoadFile("http://vigilsoft.net/democal3d/getxmlimage.php?a=paladin_chest.jpg",0,New SKNCalImageLoader())	
		'hndlr.LoadData("http://vigilsoft.net/democal3d/paladin_chest.png",0,New PngLoader())		
		
	End
	
	Method Init:Int()
		
		'If init_gl = True Then Return
		CB3D = New Cal3dB3d
		dt = New DeltaTimer(30)
			

		cam = CreateCamera()
		cam.CameraClsColor(0,0,80)
		cam.PositionEntity 0,4,-50	
		cam.RotateEntity(-90,0,0)
		light=CreateLight(1)
		light.PositionEntity 0,3,-3	
			
		mesh = CreateCube()
		txt = TText.CreateText2D()	
		Print "main: init done"
		init_gl = True
		
	End
	
	
	Method Update:int()
		

		If KeyHit(KEY_CLOSE) Or KeyHit(KEY_ESCAPE) Then Error ""
		'If init_gl  = True Then
			'Print "Has INIT WTF?"			
		'Endif
		'If init_gl = False Then 
		'	Init() 
		'	Return
		'Endif
		If CB3D <> Null then
			If CB3D.isloaded = False then
				If hndlr.IsFileLoaded("http://vigilsoft.net/democal3d/test.xsf") = True And hndlr.IsFileLoaded("http://vigilsoft.net/democal3d/testpp.xmf") = True And hndlr.IsFileLoaded("http://vigilsoft.net/democal3d/run.xaf") = True Then
					CB3D.SetSkeleton(CalCoreSkeleton(hndlr.GetLoadedFile("http://vigilsoft.net/democal3d/test.xsf")))
					CB3D.AddMesh(CalCoreMesh(hndlr.GetLoadedFile("http://vigilsoft.net/democal3d/testpp.xmf")))
					CB3D.AddAnim(CalCoreAnimation(hndlr.GetLoadedFile("http://vigilsoft.net/democal3d/run.xaf")))
					Print "Building Mesh"
					CB3D.Build()
					CB3D.CModel.getMixer().blendCycle(0,1,0)
					CB3D.isloaded = True
				Endif
			Else
				CB3D.Update(dt.delta/30)
			Endif	
		Endif
		If TestTexture = Null Then
			If hndlr.IsFileLoaded("http://vigilsoft.net/democal3d/getxmlimage.php?a=paladin_chest.jpg") = True Then
				Cimg = CalImage(hndlr.GetLoadedFile("http://vigilsoft.net/democal3d/getxmlimage.php?a=paladin_chest.jpg"))
			Endif
		Endif
			
		If Cimg <> Null Then
			If Cimg.IsFinished = False Then
			
				Cimg.Update()
			Else
				'TestTexture.ReloadAllTextures()
				EntityTexture(CB3D.B3dModel,Cimg.texture)
				For Local msh:Cal3dB3dMesh = Eachin CB3D.Meshes
					EntityTexture(msh.B3dMesh,Cimg.texture)
				Next
				EntityTexture(mesh,Cimg.texture)
			Endif
		Endif
		UpdateAsyncEvents()
		hndlr.Update()
		dt.UpdateDelta()
		' control camera
		Local cr:Float = KeyDown(KEY_LEFT)-KeyDown(KEY_RIGHT)
		Local cu:Float = KeyDown(KEY_DOWN)-KeyDown(KEY_UP)
		
		Local camin:Float = KeyDown(KEY_W)-KeyDown(KEY_S)
		Local camup:Float = KeyDown(KEY_D)-KeyDown(KEY_A)
		
		Local turnzx:Float = KeyDown(KEY_Z)-KeyDown(KEY_X)
		
		If TouchDown(0) And Not TouchDown(1)
			If Not touchBegin
				oldTouchX = TouchX()
				oldTouchY = TouchY()
				touchBegin = 1
			Endif
			lr = (TouchX() - oldTouchX) * 0.5
			ud = (-TouchY() + oldTouchY) *0.5
			oldTouchX = TouchX()
			oldTouchY = TouchY()
		Elseif TouchDown(1)
			If Not touchBegin
				oldTouchX = TouchX()
				oldTouchY = TouchY()
				touchBegin = 1
			Endif
			camup = (-TouchX() + oldTouchX) * 0.1
			camin = (-TouchY() + oldTouchY) *0.1
			oldTouchX = TouchX()
			oldTouchY = TouchY()
		Else
			touchBegin = 0
		Endif
		
		cam.MoveEntity camup,0,camin
		cam.TurnEntity cu,cr,0
		
		txt.SetText("Cam: "+camup+" "+camin+" "+cu+" "+cr+" delta: "+dt.delta)
		txt.HideEntity()
		txt.Draw(0,0)	
		
		'UpdateWorld()
		Return 0
	End	

	Method Render:Int()
	
		SetMojoEmulation()
	
		
		Return 0
	End
	
	'Method OnRender()

		'RenderWorld()
		
					
	'End

End

